//
//  DDBluetoothVC.m
//  dingdang
//
//  Created by mac mini on 16/2/17.
//  Copyright © 2016年 丁当科技. All rights reserved.
//

#import "DDBluetoothVC.h"
#import "X6_bt_a.h"
#import "Link.h"
@interface DDBluetoothVC ()
{
    NSMutableArray* LSections;
    NSArray* LSectionssort;
    mode *clk;
}
@property (weak, nonatomic) IBOutlet UITableView *perlist;
@end

@implementation DDBluetoothVC

- (void)viewDidLoad {
    NSLog(@"进入到------> DDBluetoothVC（1） - (void)viewDidLoad {");
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBarHidden = YES;
    //    self.navigationController.navigationBar.hidden = NO;
    [self addNavigatorTitle:NSLocalizedString(@"上课签到", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        [self dismissViewControllerAnimated:YES completion:nil];
        //[self.navigationController popViewControllerAnimated:YES];
    }];
    [self AddRightBtnAction:@"搜索" normal:@"" selected:@"" action:^{
        [self searchBtn];
    }];
    self.perlist.delegate = self;
    self.perlist.dataSource = self;
    [X6_bt_a getInstance];
    LSections = [NSMutableArray array];
    LSectionssort = [NSArray array];
    //[self searchBtn];
}

//- (IBAction)searchBtn:(UIButton *)sender {
- (void)searchBtn{
    NSLog(@"进入到------> DDBluetoothVC（2） - (void)searchBtn{");
    if(clk.Peripheral != nil){
        [[X6_bt_a getInstance] DisconnectBraceletp:clk.Peripheral blk:^(BOOL val){
            
        }err:^(NSError* error){
            
        }];
        sleep(2);
    }
    [LSections removeAllObjects];
    [[X6_bt_a getInstance] SearchBracelet:^(NSMutableArray* array) {
        LSections = array;
        //  NSLog(@"%@" ,LSections);
        LSectionssort = [self sortData];
        [self.perlist reloadData];
    }err:^(NSError *error){
        
    }];
}

-(NSArray*) sortData
{
    NSLog(@"进入到------> DDBluetoothVC（3） -(NSArray*) sortData");
    NSComparator cmptr = ^(id obj1, id obj2){
        int i1 = [((mode*)obj1).rssi intValue];
        int i2 = [((mode*)obj2).rssi intValue];
        if (i2 > i1) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if (i2 < i1) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    };
    return [LSections sortedArrayUsingComparator:cmptr];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"进入到------> DDBluetoothVC（4） - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView");
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"进入到------> DDBluetoothVC（5） - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section");
    return [LSectionssort count];
}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"进入到------> DDBluetoothVC（6） - (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath");
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"进入到------> DDBluetoothVC（7） - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath");
    // if(tableView == self.searchTablev)
    {
        static NSString *CellIdentifier = @"Celll";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        }
        cell.textLabel.text = ((mode*)[LSectionssort objectAtIndex:indexPath.row]).Peripheral.name;
        [cell.detailTextLabel setNumberOfLines:4];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@\n信号:%@",((mode*)[LSectionssort objectAtIndex:indexPath.row]).Peripheral.identifier.UUIDString,((mode*)[LSectionssort objectAtIndex:indexPath.row]).rssi];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"进入到------> DDBluetoothVC（8） - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath");
//    [self performSegueWithIdentifier:@"link" sender:indexPath];
//    NSLog(@"indexPath==%@",indexPath);
    Link *LinkVC = [[Link alloc]initWithNibName:@"Link" bundle:nil];
    
    clk = (mode*)[LSectionssort objectAtIndex:indexPath.row];
    LinkVC.md = clk;

    [self presentViewController:LinkVC animated:YES completion:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"进入到------> DDBluetoothVC（9） - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {");
    UIViewController *destination = segue.destinationViewController;
    NSIndexPath *index = (NSIndexPath*)sender;
    clk = (mode*)[LSectionssort objectAtIndex:index.row];
    [destination setValue:clk forKey:@"md"];
}

- (void)didReceiveMemoryWarning {
    NSLog(@"进入到------> DDBluetoothVC（10） - (void)didReceiveMemoryWarning {");
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end